from django.contrib.auth import get_user_model
from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.response import Response

from rest_framework.generics import GenericAPIView
from user.serializers import RegisterSerializer
from user.models import User
# User = get_user_model()


class RegisterView(GenericAPIView):
    serializer_class = RegisterSerializer
    def post(self,request):
        # user = User.objects.all()
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data,status=status.HTTP_201_CREATED)



from rest_framework import status
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from user.serializers import *


class UserDetailView(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserDetailSerializer

    def get_queryset(self):
        user = self.request.user
        return self.queryset

    def list(self, request, *args, **kwargs):
        user = request.user
        serializer = self.get_serializer(instance=user)
        return Response(serializer.data)

    def update(self, request, *args, **kwargs):
        instance = request.user
        serializer = self.get_serializer(instance, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            # If 'prefetch_related' has been applied to a queryset, we need to
            # forcibly invalidate the prefetch cache on the instance.
            instance._prefetched_objects_cache = {}
        return Response(serializer.data)


class PassWordView(APIView):
    def put(self, request):
        # 获取参数
        password = request.data["password"]
        # 获得请求用户
        user = User.objects.get(id=request.user.id)
        if len(password) > 20 or len(password) < 8:
            # raise Exception("密码长度需要8到20位")
            return Response({'message': '密码长度需要8～20位'}, status=status.HTTP_403_FORBIDDEN)
        # 修改密码为新密码
        user.set_password(password)
        user.save()
        # 返回数据
        return Response()


class UserLikeView(APIView):
    def post(self, request, pk):
        auther = User.objects.get(id=pk)
        user = request.user
        all_user = auther.fans.all()
        if user not in all_user:
            auther.fans.add(request.user)
            data = {
                'message':'关注成功',
                'success': True
            }
            return Response(data)
        else:
            data = {
                'message': '关注失败',
                'success': False
            }
            return Response(data)

    def delete(self, request, pk):
        auther = User.objects.get(id=pk)
        user = request.user
        fans = auther.fans.all()
        if user in fans:
            auther.fans.remove(user)
            return Response({
                'success': True,
                'message': '取消关注'
            })
        else:
            return Response({
                'success': False,
                'message': '取消关注失败'
            })

